package com.itheima.effective;

import java.io.*;

public class EffectiveDemo1 {

    public static void main(String[] args) {
        // 拿到时间毫秒值，从1970年1月1日0点0分0秒开始
        long start = System.currentTimeMillis();
        // 使用低级的字节流按照一个一个字节的方式复制视频
        try (
                InputStream fis = new FileInputStream("D:\\mp4s\\first.mp4");
                OutputStream fos = new FileOutputStream("D:\\mp4s\\first-copy1.mp4"))
        {
            int b = 0;
            while ((b = fis.read()) != -1) {
                fos.write(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("低级的字节流复制完成，耗时：" + (end - start) + "毫秒");

        // 使用低级的字节流按照字节数组的方式复制视频
        start = System.currentTimeMillis();
        try (
                InputStream fis = new FileInputStream("D:\\mp4s\\first.mp4");
                OutputStream fos = new FileOutputStream("D:\\mp4s\\first-copy2.mp4"))
        {
            byte[] buffer = new byte[8192];
            int len = 0;
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        end = System.currentTimeMillis();
        System.out.println("低级的字节流按照数组复制完成，耗时：" + (end - start) + "毫秒");


        // 使用高级的缓冲字节流一个字节一个字节的方式复制视频
        start = System.currentTimeMillis();
        try (
                InputStream fis = new FileInputStream("D:\\mp4s\\first.mp4");
                OutputStream fos = new FileOutputStream("D:\\mp4s\\first-copy3.mp4");
                // 把低级的字节输入流包装成高级的缓冲字节输入流
                InputStream bis = new BufferedInputStream(fis);
                OutputStream bos = new BufferedOutputStream(fos))
        {
            int b = 0;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        end = System.currentTimeMillis();
        System.out.println("高级的缓冲字节流按照一个字节一个字节的方式复制完成，耗时：" + (end - start) + "毫秒");

        // 使用高级的缓冲字节流按照字节数组的方式复制视频
        start = System.currentTimeMillis();
        try(
                InputStream fis = new FileInputStream("D:\\mp4s\\first.mp4");
                OutputStream fos = new FileOutputStream("D:\\mp4s\\first-copy4.mp4");
                InputStream bis = new BufferedInputStream(fis);
                OutputStream bos = new BufferedOutputStream(fos);)
        {
            byte[] buffer = new byte[8192];
            int len = 0;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        end = System.currentTimeMillis();
        System.out.println("高级的缓冲字节流按照字节数组的方式复制完成，耗时：" + (end - start) + "毫秒");
    }
}
